<template>
  <div class="wrapper">

    <div style="width: 500px; height: 460px; background-color: white; border-radius: 10px;
    margin: 100px auto;padding: 35px 40px 40px">
      <div style="margin: 5px;text-align: center;font-size: 30px;font-weight: bold;color: dodgerblue">注 册</div>
      <el-form :model="user"  :rules="rules" ref="registerForm">
        <el-form-item label="账号" prop="userid">
          <el-input placeholder="请输入帐号" prefix-icon="el-icon-user" size="medium" v-model="user.usersId"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input placeholder="请输入密码" prefix-icon="el-icon-lock" size="medium" v-model="user.usersPassword"></el-input>
        </el-form-item>
        <el-form-item label="再次输入密码" prop="confirmPassword">
          <el-input placeholder="请确认密码" size="medium" style="margin: 5px 0" prefix-icon="el-icon-lock" show-password v-model="user.confirmPassword"></el-input>
        </el-form-item>
        <el-form-item label="用户类型" prop="usersRole">
          <el-radio-group v-model="user.usersRole" >
            <el-radio label="学生" style="margin: 5px 30px 15px 20px;">学生</el-radio>
            <el-radio label="老师" style="margin: 5px 30px 15px 20px;">老师</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item style="margin: 5px 0; text-align: right">
          <el-button type="primary" size="small"  autocomplete="off" @click="register">注册</el-button>
          <el-button type="warning" size="small"  autocomplete="off" @click="$router.push('/login')">返回登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>

</template>

<script>
export default {
  name: "Register",
  data(){
    return{
      user: {
        usersRole:''
      },
      rules: {
        usersId: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { pattern: /^[0-9]+$/, message: '请规范您得输入', trigger: 'blur' },
          { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' }
        ],
        usersPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^[A-Za-z0-9_]+$/, message: '密码只能由数字、字母、下划线组成', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^[A-Za-z0-9_]+$/, message: '密码只能由数字、字母、下划线组成', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        usersRole: [
          {required:true,message:"请选择用户类型！",trigger:'change'}
        ]
      }
    }
  },
  methods: {
    register() {



      this.$refs['registerForm'].validate((valid) => {
        if (valid) {  // 表单校验合法
          if (this.user.usersPassword !== this.user.confirmPassword) {
            this.$message.error("两次输入的密码不一致")
            return false
          }
          if (this.user.usersRole == null) {
            this.$message.error("请选择身份！")
            return false
          }
          this.request.post("/Users/register", this.user)
              .then(res => {
                // 在这里处理成功响应
                if (res.code === '200') {
                  this.$message.success("注册成功")
                } else {
                  this.$message.error(res.msg)
                }
              })
              .catch(error => {
                // 在这里处理错误响应
                if (error.response.status === 400) {
                  this.$message.error("格式有误！")
                }
              })
        }
      });
    }
  }

}
</script>

<style>
.wrapper {
  height: 100vh;
  /*background-image: linear-gradient(to bottom right, #FC466B , #3F5EFB);*/
  overflow: hidden;
}
</style>